/*
 * @Author: shiyue 2647472992@qq.com
 * @Date: 2024-02-16 16:09:02
 * @LastEditors: shiyue 2647472992@qq.com
 * @LastEditTime: 2024-02-17 11:20:09
 * @FilePath: /vscodecpp/test-2-16/ServeCal.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include "Protocol.hpp"
enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other
};
class ServeCal
{
    public:
    ServeCal()
    {
    }
    ~ServeCal()
    {
    }
    Respond CalculatorMethod(Request &quest)
    {
        Respond re;
        switch (quest.op_)
        {
        case '+':
            re.result = quest.x_ + quest.y_;
            break;
        case '-':
            re.result = quest.x_ - quest.y_;
            break;
        case '*':
            re.result = quest.x_ * quest.y_;
            break;
        case '/':
        {
            if (quest.y_ == 0)
                re.code = Div_Zero;
            else
                re.result = quest.x_ / quest.y_;
        }
        break;
        case '%':
        {
            if (quest.y_ == 0)
                re.code = Mod_Zero;
            else
                re.result = quest.x_ % quest.y_;
        }
        break;
        default:
            re.code = Other;
            break;
        }
        return re;
    }
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool check = Decode(package, &content);
        if (!check)
            return "错误1";
        Request re;
        check = re.Deserialize(content);
        if (!check)
            return "错误2";
        Respond answer = CalculatorMethod(re);
        answer.Serialize(&content);
        content = Encode(content);
        return content;
    }
};